package com.gwm.item.controller;
import com.gwm.common.utils.BeanHelper;
import com.gwm.item.dto.CategoryDTO;
import com.gwm.item.service.TbCategoryService;
import com.gwm.item.pojo.TbCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/category")
public class CategoryController {


    @Autowired
    private TbCategoryService categoryService;


    @GetMapping(value = "/of/parent",name = "根据parentid获取分类信息")
    public ResponseEntity<List<CategoryDTO>> findCategoryListByParentId(@RequestParam("pid") Long pid){
       List<CategoryDTO> categoryList =  categoryService.findCategoryListByParentId(pid);
       return ResponseEntity.ok(categoryList);
    }

    @GetMapping(value = "/of/brand",name = "根据品牌id获取分类信息")
    public ResponseEntity<List<CategoryDTO>> findCategoryListByBrandId(@RequestParam("id") Long brandId){
        List<CategoryDTO> categoryList =  categoryService.findCategoryListByBrandId(brandId);
        return ResponseEntity.ok(categoryList);
    }

    @GetMapping(value = "/findCategoryListByIds",name = "根据分类ids获取分类")
    public ResponseEntity<List<CategoryDTO>> findCategoryListByIds(@RequestParam("ids") List<Long> ids){
        Collection<TbCategory> tbCategoryList = categoryService.listByIds(ids);

        List<CategoryDTO> categoryDTOList = tbCategoryList.stream().map(category -> {
            return BeanHelper.copyProperties(category, CategoryDTO.class);
        }).collect(Collectors.toList());
        return ResponseEntity.ok(categoryDTOList);
    }

}
